package com.itheima.crm.controller;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.ServletRequestDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import com.itheima.common.utils.Page;
import com.itheima.crm.pojo.Application;
import com.itheima.crm.pojo.BaseDict;
import com.itheima.crm.pojo.Meeting;
import com.itheima.crm.pojo.MeetingPlace;
import com.itheima.crm.pojo.RiZhi;
import com.itheima.crm.pojo.YuanGong;
import com.itheima.crm.service.ApplicationService;
import com.itheima.crm.service.BaseDictService;
import com.itheima.crm.service.MeetingPlaceService;
import com.itheima.crm.service.MeetingService;
import com.itheima.crm.service.RiZhiService;
import com.itheima.crm.service.YuanGongService;

/**
 * 客户管理
 * 
 * @author lx
 *
 */
@Controller
public class MeetingController {

	@Autowired
	private MeetingService meetingService;
	@Autowired
	private ApplicationService applicationService;
	@Autowired
	private BaseDictService baseDictService;
	@Autowired
	private YuanGongService yuangongService;
	@Autowired
	private MeetingPlaceService meetingPlaceService;
	@Autowired
	private RiZhiService rizhiService;
	
	public MeetingService getMeetingService() {
		return meetingService;
	}

	public void setMeetingService(MeetingService meetingService) {
		this.meetingService = meetingService;
	}

	public ApplicationService getApplicationService() {
		return applicationService;
	}

	public void setApplicationService(ApplicationService applicationService) {
		this.applicationService = applicationService;
	}

	public BaseDictService getBaseDictService() {
		return baseDictService;
	}

	public void setBaseDictService(BaseDictService baseDictService) {
		this.baseDictService = baseDictService;
	}

	public YuanGongService getYuangongService() {
		return yuangongService;
	}

	public void setYuangongService(YuanGongService yuangongService) {
		this.yuangongService = yuangongService;
	}

	public MeetingPlaceService getMeetingPlaceService() {
		return meetingPlaceService;
	}

	public void setMeetingPlaceService(MeetingPlaceService meetingPlaceService) {
		this.meetingPlaceService = meetingPlaceService;
	}

	public RiZhiService getRizhiService() {
		return rizhiService;
	}

	public void setRizhiService(RiZhiService rizhiService) {
		this.rizhiService = rizhiService;
	}

	@RequestMapping(value = "/HuiYiAnPai")
	public ModelAndView HuiYi(Meeting meeting, YuanGong yuangong, Model model, HttpServletResponse response,
			HttpServletRequest request) {
		List<BaseDict> OrganizationType = baseDictService.selectBaseDictListByCode("002");
		List<MeetingPlace> meetingroom=meetingPlaceService.selectMeetingPlace();
		System.out.println(meetingroom.get(0).toString());
		System.out.println(meetingroom.get(1).toString());
		model.addAttribute("meetingroom", meetingroom);
		model.addAttribute("OrganizationType", OrganizationType);
		

		ModelAndView a = new ModelAndView();
		Page<Meeting> page = meetingService.selectMeetingList(meeting);

		a.setViewName("Meeting");

		a.addObject("page", page);
		return a;

	}

	@RequestMapping(value = "/HuiYiAnPaiSecond/newMeeting.action")
	public @ResponseBody String insertMeeting(Application application, Meeting meeting,HttpServletRequest request) {

		meetingService.insertMeeting(meeting, application);
		// 修改会议状态
		applicationService.updateApplicationResult(application.getId());
		
		RiZhi a=new RiZhi();
		request.getSession().getAttribute("ygid");
		a.setYgid((Integer)request.getSession().getAttribute("ygid"));
		a.setYgname((String)request.getSession().getAttribute("ygname"));
		a.setInsertdate(new Date());
		System.out.println(a.getInsertdate());
		a.setDetial("新建会议");
		rizhiService.insertApplication(a);
		return "OK";
	}

	@RequestMapping(value = "/HuiYiAnPai/delete.action")
	public @ResponseBody String deleteMeeting(String  id,HttpServletRequest request) {
		System.out.println(id);
		RiZhi a=new RiZhi();
		request.getSession().getAttribute("ygid");
		a.setYgid((Integer)request.getSession().getAttribute("ygid"));
		a.setYgname((String)request.getSession().getAttribute("ygname"));
		a.setInsertdate(new Date());
		System.out.println(a.getInsertdate());
		a.setDetial("删除会议/撤销会议");
		rizhiService.insertApplication(a);
		meetingService.deleteMeeting(Integer.parseInt(id));
		return "OK";
	}

	@RequestMapping(value = "/HuiYiAnPai/saveUpdate.action")
	public @ResponseBody String updateMeeting(Meeting meeting,HttpServletRequest request) {
		meetingService.updateMeetingById(meeting);
		RiZhi a=new RiZhi();
		request.getSession().getAttribute("ygid");
		a.setYgid((Integer)request.getSession().getAttribute("ygid"));
		a.setYgname((String)request.getSession().getAttribute("ygname"));
		a.setInsertdate(new Date());
		System.out.println(a.getInsertdate());
		a.setDetial("更新存会议信息");
		rizhiService.insertApplication(a);
		return "OK";
	}

	@RequestMapping(value = "/HuiYiAnPai/edit.action")
	public @ResponseBody Meeting editMeeting(Integer id) {
		return meetingService.selectMeetingById(id);
	}

	// 点击添加人员进入dialog获取员工中内容
	@RequestMapping(value = "/HuiYiAnPai/joinpeople.action")
	public @ResponseBody String joinpeopleedit(HttpServletRequest request,YuanGong yuangong) {
		
	/*	Integer id=(Integer) request.getAttribute("id");
		model.addAttribute("id", id);
		model.addAttribute("param", yuangong.getId());
		*/
		System.out.println(yuangong.getId());
		
		
		 HttpSession session=request.getSession(false);
		session.setAttribute("mid", yuangong.getId());
		
		
		return yuangongService.selectYuanGongAll(yuangong);

	}
	@RequestMapping(value = "/HuiYiAnPai/savejoin.action")
	public @ResponseBody String joinpeople(String[]array,Integer mid,HttpServletRequest request) {

		String S="";
	for(int i=0;i<array.length;i++) {
		if(array[i]!="") {
		System.out.println(array[i]);
		Integer a=Integer.parseInt(array[i]);
		YuanGong gong=yuangongService.selectYuanGongById(a);
		String name=gong.getYgname();
		System.out.println(name);
		S+=name;
		S+=",";
		
		}
		
	}
	System.out.println(S);
	S = S.substring(0, S.length()-1) + "。";
		
		/*for(int i=0;i<id.length;i++) {
			System.out.println(id[i]);
			YuanGong gong=yuangongService.selectYuanGongById(id[i]);
			String name=gong.getYgname();
			a+=",";
			a+=name;
		}*/
		
		
		
	RiZhi a=new RiZhi();
	request.getSession().getAttribute("ygid");
	a.setYgid((Integer)request.getSession().getAttribute("ygid"));
	a.setYgname((String)request.getSession().getAttribute("ygname"));
	a.setInsertdate(new Date());
	System.out.println(a.getInsertdate());
	a.setDetial("添加会议人员");
	rizhiService.insertApplication(a);
		
		meetingService.joinpeople(S,mid);
		return "OK";

	}
	
	
	

	

	@RequestMapping(value = "/HuiYiAnPaiSecond/edit.action")
	public @ResponseBody Application editA(Integer id) {
		return applicationService.selectApplicationById(id);
	}

	@RequestMapping(value = "/HuiYiAnPaiSecond")
	public ModelAndView HuiYiList(Application application, Model model, HttpServletResponse response,
			HttpServletRequest request) {

		HttpSession session=request.getSession(false);
		String zhiwei = (String) session.getAttribute("zhiwei");
		if (!zhiwei.equals("1")) {
		List<BaseDict> typeType = baseDictService.selectBaseDictListByCode("004");
		List<BaseDict> OrganizationType = baseDictService.selectBaseDictListByCode("002");

		List<BaseDict> resultType = baseDictService.selectBaseDictListByCode("006");
		List<MeetingPlace> meetingroom=meetingPlaceService.selectMeetingPlace();
		System.out.println(meetingroom.get(0).toString());
		System.out.println(meetingroom.get(1).toString());
		model.addAttribute("meetingroom", meetingroom);
		model.addAttribute("typeType", typeType);
		model.addAttribute("OrganizationType", OrganizationType);

		model.addAttribute("resultType", resultType);
		
		
		ModelAndView a = new ModelAndView();
		Page<Application> page = applicationService.selectHuiYi(application);
		a.setViewName("MeetingSecond");

		a.addObject("page", page);
		return a;}
		else{
			
			ModelAndView a=new ModelAndView();
			
			a.setViewName("error");

		     return a;
		}
	

	}
	
	

	@InitBinder
	protected void init(HttpServletRequest request, ServletRequestDataBinder binder) {
		SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
		dateFormat.setLenient(false);
		binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, false));
	}

}
